package 同步代码块;

public class text {
    public static void main(String[] args) {
        //创建线程对象
        MyThread myThread = new MyThread();
        //创建三个线程并开启
        new Thread(myThread,"窗口1").start();
        new Thread(myThread,"窗口2").start();
        new Thread(myThread,"窗口3").start();

    }
}
class MyThread implements Runnable {
   private int tick = 10;
   Object lock = new Object();
   public void run() {
       while (true) {
           synchronized (lock) {//同步代码块加一把锁，锁住共享资源

                   try {
                       Thread.sleep(100);
                   }
                   catch (InterruptedException e) {
                       e.printStackTrace();
                       break;

                   }
                   if (tick > 0) {
                       tick--;
                       System.out.println(Thread.currentThread().getName() + "还剩下" + tick + "张票");
                   }
               }
           }
       }
   }
